package com.resource.controller;

import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.resource.utils.ResultBody;
import com.resource.utils.ServiceResultHandler;
import org.springframework.web.bind.annotation.*;

import com.resource.dto.CustomerLinkmanSaveDto;
import com.resource.query.CustomerLinkmanQuery;
import com.resource.service.CustomerLinkmanService;

/**
 * 客户联系人管理
 * 
 * @Author: xiongbo.wan
 * @Date: 14:16 2020/9/22 0022
 */
@RestController
@RequestMapping("/customer/linkman")
public class CustomerLinkmanController {

	@Resource
	private CustomerLinkmanService customerLinkmanService;

	/**
	 * [新增]
	 **/
	@PostMapping
	public ResultBody create(CustomerLinkmanSaveDto dto) {
		return ServiceResultHandler
				.process((Consumer<Object>) (o) -> customerLinkmanService.create((CustomerLinkmanSaveDto) o), dto);
	}

	/**
	 * [刪除]
	 **/
	@DeleteMapping("/{id}")
	public ResultBody remove(@PathVariable Long id) {
		return ServiceResultHandler.process((Consumer<Object>) (o) -> customerLinkmanService.remove((Long) o), id);
	}

	/**
	 * [更新]
	 **/
	@PutMapping("/{id}")
	public ResultBody modify(CustomerLinkmanSaveDto dto, @PathVariable String id) {
		return ServiceResultHandler.process((BiFunction<Object, Object, Object>) (o, b) -> customerLinkmanService
				.modify((CustomerLinkmanSaveDto) o, (String) b), dto, id);
	}

	/**
	 * [查询] 根据主键 id 查询
	 **/
	@GetMapping("/{id}")
	public ResultBody load(@PathVariable Long id) {
		return ServiceResultHandler.process((Function<Object, Object>) o -> customerLinkmanService.load((Long) o), id);
	}

	/**
	 * [查询] 不分页查询
	 **/
	@GetMapping("/all")
	public ResultBody list(CustomerLinkmanQuery customerLinkmanQuery) {
		return ServiceResultHandler.process(
				(Function<Object, Object>) o -> customerLinkmanService.list((CustomerLinkmanQuery) o),
				customerLinkmanQuery);
	}

	/**
	 * 数据外发修改联系人
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/send")
	public ResultBody modifyByContract(HttpServletRequest request) {
		String id = request.getParameter("DATA_63");
		String email = request.getParameter("DATA_34");
		String mobilePhoneNumber = request.getParameter("DATA_51");
//	todo 生产	String id = request.getParameter("DATA_71");
//		String email = request.getParameter("DATA_34");
//		String mobilePhoneNumber = request.getParameter("DATA_51");
		CustomerLinkmanSaveDto dto = CustomerLinkmanSaveDto.builder().linkmanId(id).email(email)
				.mobilePhoneNumber(mobilePhoneNumber).build();
		return ServiceResultHandler.process(
				(Consumer<Object>) (o) -> customerLinkmanService.modifyByContract((CustomerLinkmanSaveDto) o), dto);
	}

}
